package cn.mapway.tools.rpc.model;

import lombok.Data;
import org.nutz.lang.Lang;

import java.util.HashMap;
import java.util.Map;

/**
 * ExportContext
 *
 * @author zhangjianshe@gmail.com
 */
@Data
public class ExportContext {
    private static ExportContext gExportContext;
    private Map<String, ClassDefination> clazzList;

    protected ExportContext() {
        clazzList = new HashMap<>();
    }

    public static ExportContext get() {
        if (gExportContext == null) {

            gExportContext = new ExportContext();
        }
        return gExportContext;
    }

    public static void print() {
        System.out.println("==========================parsed data=================================");
        for (String key : get().getClazzList().keySet()) {
            ClassDefination classDefination = get().sureClassDefination(key);
            if (Lang.isNotEmpty(classDefination.typeParameters)) {
                String templates = Lang.concat(",", classDefination.typeParameters).toString();
                System.out.println(key + "[" + templates + "]");
            } else {
                System.out.println(key);
            }

            for (FieldDefination fd : classDefination.getFields()) {
                if (!fd.isStatic) {
                    System.out.println("\t" + fd.typeName + "\t" + fd.name);
                }
            }
        }
    }

    /**
     * 从环境中获取类型定义
     *
     * @param className
     * @return da
     */
    public ClassDefination sureClassDefination(String className) {
        if (clazzList.containsKey(className)) {
            return clazzList.get(className);
        }
        ClassDefination classDefination = new ClassDefination();
        clazzList.put(className, classDefination);
        return classDefination;
    }

    /**
     * 是否已经解析过了
     *
     * @param className
     *
     * @return  ee
     */
    public boolean hasParsed(String className) {
        return clazzList.containsKey(className);
    }
}
